import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
// 引入Login登录页面
import Login from '@/views/Login'
// 引入注册页
import Reg from '@/views/Reg'

// 引入首页
import Layout from '@/views/Layout'

// 引入二级路由
// 引入主页
import Home from '@/views/Home'
// 引入文章分类页
import ArtCategory from '@/views/ArtCategory'
// 引入文章列表页
import ArtList from '@/views/ArtList'
// 引入基本资料页
import UserInfo from '@/views/UserInfo'
// 引入更换头像页
import ChangeAvatar from '@/views/ChangeAvatar'
// 引入重置密码页
import ResetPwd from '@/views/ResetPwd'

Vue.use(VueRouter)

const routes = [
  // 登录页
  {
    path: '/login',
    component: Login
    // name: Login
  },
  // 注册页
  {
    path: '/reg',
    component: Reg
    // name: Reg
  },
  // 首页
  {
    path: '/',
    component: Layout,
    name: Layout,
    children: [
      {
        path: '/',
        component: Home
        // name: Home
      },
      {
        path: '/artcategory',
        component: ArtCategory
        // name: ArtCategory
      },
      {
        path: '/artlist',
        component: ArtList
        // name: ArtList
      },
      {
        path: '/userinfo',
        component: UserInfo
        // name: UserInfo
      },
      {
        path: '/changeavatar',
        component: ChangeAvatar
        // name: ChangeAvatar
      },
      {
        path: '/resetpwd',
        component: ResetPwd
        // name: ResetPwd
      }
    ]
  }

]

const router = new VueRouter({
  routes
})

// 浏览白名单
const WHITE_LIST = ['Login', 'Reg']
// 路由前置守卫
router.beforeEach((to, from, next) => {
  // console.log(to)
  if (WHITE_LIST.includes(to.path)) {
    next()
  } else {
    // 如果没有token令牌
    if (localStorage.getItem('token')) {
      next()
    } else {
      // 删除token
      localStorage.removeItem('token')
      // 跳转到登录页
      next({ name: Login })
    }
  }
})

export default router
